﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using Maticsoft.Common;
using CustomsSystem.Model;

namespace WEB.Bank
{
    public partial class BankInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
            //gridViewClass.GroupRows2(gv, 1,8);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery1_Click(object sender, EventArgs e)
        {
            bind();
        }

        /// <summary>
        /// 绑定
        /// </summary>
        protected void bind()
        {
            string strWhere = " CustomerName like '%" + tbQuery1.Text.Trim() + "%'";
            CustomsSystem.BLL.BankManage bll = new BankManage();
            DataSet ds;
            ds = bll.GetList(strWhere);
            if (ds.Tables[0].Rows.Count < 1)
            { lbj.Text = "无银行信息"; }
            else
            { lbj.Text = ""; }
            gv.DataSource = ds;
            gv.DataBind();

        }

        /// <summary>
        /// 编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv.EditIndex = e.NewEditIndex;
            bind();
        }

        /// <summary>
        /// 取消编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv.EditIndex = -1;
            bind();
        }

     
      

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string Phone = ((TextBox)gv.Rows[e.RowIndex].FindControl("tbPhone")).Text.Trim();
            string Fax = ((TextBox)gv.Rows[e.RowIndex].FindControl("tbFax")).Text.Trim();
            string Bank = ((TextBox)gv.Rows[e.RowIndex].FindControl("tbBank")).Text.Trim();
            string BankNo = ((TextBox)gv.Rows[e.RowIndex].FindControl("tbBankNo")).Text.Trim();
            string Remarks = ((TextBox)gv.Rows[e.RowIndex].FindControl("tbRemarks")).Text.Trim();
            int ID = int.Parse (((HiddenField)gv.Rows[e.RowIndex].FindControl("hfID")).Value .ToString ());
            int CID = int.Parse(((HiddenField)gv.Rows[e.RowIndex].FindControl("hfCID")).Value.ToString());
            CustomsSystem.Model.Cu_Bank model = new CustomsSystem.Model.Cu_Bank();
            model.CID = CID;
            model.Phone = Phone;
            model.Fax = Fax;
            model.Bank = Bank;
            model.BankNO = BankNo;
            model.Remarks = Remarks;
            model.ID = ID;
            CustomsSystem.BLL.BankManage BKM= new BankManage();
            BKM.Update(model);

            gv.EditIndex = -1;
            bind();
            Maticsoft.Common.MessageBox.Show(this, "保存成功！"); 

        }

        protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Session["CustomerName"] = ((TextBox)gv.Rows[e.NewSelectedIndex].FindControl("tbCustomerName")).Text.Trim();
            Response.Redirect("BankAdd.aspx");
        
        }

        protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {                           

            int ID = int.Parse(((HiddenField)gv.Rows[e.RowIndex].FindControl("hfID")).Value.ToString());
            CustomsSystem.BLL.BankManage BKM = new BankManage();
            if (BKM.Delete(ID))
            {
                MessageBox.ShowAndRedirect(this, "删除成功！", "BankInfo.aspx");
            }
            else { MessageBox.ShowAndRedirect(this, "未知原因导致删除失败！", "BankInfo.aspx"); }
        }

        /// <summary>
        /// 允许分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //换页功能
            this.gv.PageIndex = e.NewPageIndex;
            //绑定GridView
            gv.DataBind();
            TextBox tb = (TextBox)gv.BottomPagerRow.FindControl("tbinPageNum");
            tb.Text = (gv.PageIndex + 1).ToString();
            bind();
        }




        /// <summary>
        /// 跳页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                gv.DataBind();

                TextBox tb = (TextBox)gv.BottomPagerRow.FindControl("tbinPageNum");
                int num = Int32.Parse(tb.Text.Trim());

                GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                gv_PageIndexChanging(null, ea);
            }
            catch { MessageBox.Show(this, "请输入数字页码"); }

        }


        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetCustomersName(string prefixText, int count)
        {
            CustomerManage cusManage = new CustomerManage();
            List<string> strArray = new List<string>();
            //string[] strArray = { "中国石油公司","中顺洁柔","中山技术",
            //"捷达捷达捷达达","时代复分","幅度为爱"};
            List<Customer> cusList = cusManage.GetModelList("");
            foreach (Customer aCus in cusList)
            {
                strArray.Add(aCus.CustomerName);
            }
            return (from m in strArray
                    where m.Contains(prefixText)
                    select m).Take(count).ToArray();
        }
        

       

    }
}